package com.apimarket.controller;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.apimarket.entity.Publish;
import com.apimarket.service.PublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.alibaba.fastjson.JSON;

/**
 * xuyang
 */
@RestController
@RequestMapping("publish")
@CrossOrigin
public class PublishController {
    @Autowired
    private PublishService service;

    @GetMapping("list")
    public String queryLists(Publish entity){
            List<Publish> list = service.queryList(entity);
            String result = JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect);
            return result;
    }

    @PostMapping("add")
    public ResponseEntity<List<Publish>> insert(@RequestBody Publish entity){
        service.insert(entity);
        return ResponseEntity.ok(service.queryList(new Publish()));
    }
    @PutMapping("update")
    public ResponseEntity<List<Publish>> update(@RequestBody Publish entity){
        service.update(entity);
        return ResponseEntity.ok(service.queryList(new Publish()));
    }
    @DeleteMapping("del/{id}")
    public ResponseEntity<List<Publish>> delete(@PathVariable("id") Integer id){

        //todo 设置删除
        service.delete(id);
        return ResponseEntity.ok(service.queryList(new Publish()));

    }
}
